<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="msapplication-tap-highlight" content="no">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>用户注册</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/bootstrap-4.2.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/user/SUI_Mobile/sm.min.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/user/SUI_Mobile/sm-extend.min.css">
  <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/user/css/common.css">
  <style type="text/css">
    .label{
      width: 130px;
    }    
    .list-block{
      /* padding: 5px 0px; */
    }
    ul::before,ul::after{
      border: 1px solid #fff;
    }
    /* input自动填入，input变黄色 */
    input:-webkit-autofill { 
      box-shadow: 0 0 0px 1000px white inset !important;
    }
    label[class="error"]{
      color:#f00;
      border:0px solid red;
      display:none;
      margin-bottom:0px;
      margin-left:130px;
    }
		.mainContent{
		  height: 100%;
		  padding: 3px;
		  border: 0px solid #0f0;
		  /*设置滚动条*/
		  overflow-x: hidden;
		  overflow-y: scroll;
		  /*解决ios上滑动不流畅*/
		  -webkit-overflow-scrolling: touch;
		  /*纵向超出部分将会隐藏，即滚动条部分被挤出可视区域*/
		}
		/*设置滚动条，谷歌内核webkit*/
		.container ::-webkit-scrollbar {
		    display: none;
		}
		/*设置滚动条，IE或者Firefox*/
		.container {
		    -ms-overflow-style: none; /*IE 10+*/
		    overflow: -moz-scrollbars-none; /*Firefox*/
		}
  </style>
</head>
<body>
  <div class="container">
    <header class="bar bar-nav header sticky-top">
      <a class="button button-link button-nav pull-left back" href="#" onclick="history.go(-1)">
        <span class="icon icon-left"></span>返回
      </a>
      <h1 class='title'>用户注册</h1>
    </header>
    <!-- 中间，记录显示 -->
    <div class="mainContent">
      <form class="list-block" id="registerUserForm">
        <ul>
          <li>
            <div class="item-content">
              <div class="item-inner" style="border:0px solid red">
                <div class="label">用户名</div>
                <div class="item-input">
                  <input type="text" id="username" name="username" class="username" readonly>
                </div>
              </div>
            </div>
            <label id="username-error" class="error" for="username">这是必填字段</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">密码</div>
                <div class="item-input">
                  <input type="password" id="password" name="password" class="password" readonly>
                </div>
              </div>
            </div>
            <label id="password-error" class="error" for="password">这是必填字段</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">确认密码</div>
                <div class="item-input">
                  <input type="password" id="confirmPassword" name="confirmPassword" class="confirmPassword" readonly>
                </div>
              </div>
            </div>
            <label id="confirmPassword-error" class="error" for="confirmPassword">二次密码输入不正确</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">真实姓名</div>
                <div class="item-input">
                  <input type="text" name="realname" class="realname">
                </div>
              </div>
            </div>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">联系方式</div>
                <div class="item-input">
                  <input type="text" name="cellphone" class="cellphone">
                </div>
              </div>
            </div>
            <label id="cellphone-error" class="error" for="cellphone">这是必填字段</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">邮箱</div>
                <div class="item-input">
                  <input type="email" name="email" class="email">
                </div>
              </div>
            </div>
            <label id="email-error" class="error" for="email">请输入有效的电子邮件地址</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">性别</div>
                <div class="item-input">
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="man" name="gender" class="custom-control-input man" value="1" checked>
                    <label class="custom-control-label" for="man" style="padding-top:4px;">男</label>
                  </div>
                  <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" id="woman" name="gender" class="custom-control-input woman" value="0">
                    <label class="custom-control-label" for="woman" style="padding-top:4px;">女</label>
                  </div>
                </div>
              </div>
            </div>
          </li>
          <li class="align-top">
            <div class="item-content">
              <div class="item-inner" style="border-bottom: 0.5px solid #E7E7E7;margin-bottom: 5px;">
                <div class="label">个人简介</div>
                <div class="item-input">
                  <textarea class="resume"></textarea>
                </div>
              </div>
            </div>
          </li>
        </ul>
      </form>
      <div class="content-block">
        <div class="row">
          <div class="col-50"><a href="javascript:;" class="button button-big button-fill button-danger resetBtn">重置</a></div>
          <div class="col-50"><a href="javascript:;" class="button button-big button-fill button-success confirmBtn">确定</a></div>
        </div>
      </div>
      <div class="modal toast modal-out" style="display: block;margin-top:-120px; margin-left: -100px;"></div>
    </div>
  </div> <!-- end of container -->

  <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.3.1.min.js" charset="utf-8"></script>
  <script type='text/javascript' src='${pageContext.request.contextPath }/user/SUI_Mobile/zepto.min.js' charset='utf-8'></script>
  <script type='text/javascript' src='${pageContext.request.contextPath }/user/SUI_Mobile/sm-extend.min.js' charset='utf-8'></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.validate.min.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/js/messages_zh.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/bootstrap-4.2.1/js/bootstrap.min.js" charset="utf-8"></script>
  <script>
    $(function () {
    	// 重置 
      $('.resetBtn').on('click', function(){
    	  resetRegisterForm()
      })
      // 验证
      jQuery.validator.setDefaults({
    	  success:'valid'
      })
      $('#registerUserForm').validate({
    	  rules:{
    		  username:{
    			  required:true,
    			  remote:{
    				  url:"/LostAndFound/userRegister?method=checkUsername",
    				  type:"post",
    				  dataType:"json",
    				  data:{
    					  username:function(){
    						  return $(".username").val()
    					  }
    				  }
    			  }
    		  },
    		  password:{
    			  required:true,
    			  minlength:6
    		  },
    		  confirmPassword:{
    			  equalTo:password
    		  },
    		  cellphone:{
    			  required:true,
    		  }
    	  },
    	  messages:{
    		  username:{
    			  remote:"用户名不可用"
    		  },
    		  confirmPassword:{
    			  equalTo:"两次输入密码不一致"
    		  }
    	  }
      })
      // 点击 确定 ，验证
      $('.confirmBtn').on('click',function () {
    	  if($('#registerUserForm').valid()){
    		  regeisterUser()
    	  }
      })
    })
    /* 重置注册输入框 */
    function resetRegisterForm () {
    	$('.username').val('')
    	$('.password').val('')
    	$('.confirmPassword').val('')
    	$('.cellphone').val('')
    	$('.realname').val('')
    	$('.email').val('')
    	$('.resume').val('')
    }
    // 设置表单不自动输入已经记住的用户账号密码 ,延缓一秒钟执行
    setTimeout(function removeReadonly(){
        $('.username').removeAttr("readonly")
        $('.password').removeAttr("readonly")
        $('.confirmPassword').removeAttr("readonly")
    },1000);
    // 显示提示，然后跳转页面
    function showToastByTime ( successFlag ) {
      var info = null;
      if( successFlag ){
        info = '注册成功'
      }else {
        info = '注册失败'
      }
      clearTimeout(timer)
      $('.toast').text(info)    
      $('.toast').addClass('modal-in')
      $('.toast').removeClass('modal-out')
      var timer = window.setTimeout(function () {
        $('.toast').addClass('modal-out')
        $('.toast').removeClass('modal-int')
        window.location.href="/LostAndFound/user/login.jsp";
      }, 2000)
    }
    // 注册
    function regeisterUser () {
        var username = $('.username').val()
        var password = $('.confirmPassword').val()
        var cellphone = $('.cellphone').val()
        var realname = $('.realname').val()
        var email = $('.email').val()
        var resume = $('.resume').val()
        var gender = $("input:radio[name='gender']:checked").val()
        $.post(
        	"/LostAndFound/userRegister?method=regeisterUser",
        	{
        		"username":username,
        		"password":password,
        		"cellphone":cellphone,
        		"realname":realname,
        		"email":email,
        		"resume":resume,
        		"gender":gender
        	},
        	function(data){
        		showToastByTime(data.success)
        	},
        	"json"
        );
    }
  </script>
</body>
</html>